/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-26 08:56:41
 */
import { autorun } from 'mobx';
import { AmbientLight, DirectionalLight, LinearSRGBColorSpace, PointLight, Vector3 } from 'three';
import ThreeBase from 'three-base';
import threeUtils from 'three-base/threeUtils';
import People from './component3d/People/People';
import Transition from './component3d/Transition';
import Warehouse from './component3d/Warehouse/Warehouse';
import homeStore from './homeStore';
/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
    constructor() {
        super();
        window.homeRenderer = this;
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight('#fff', 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight('#fff', 1.5);
        pointLight.position.set(50, 40, 50);
        pointLight.shadow.bias = -0.001;
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight('#fff', 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.bias = -0.001;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();
        // this.needRender = false;

        // 开发模式时还原记录的位置
        if (threeUtils.isDev && localStorage.lookAt) {
            this.resetLookAt({
                ...JSON.parse(localStorage.lookAt),
            });
        }

        // 开发模式时记录当前的位置
        if (threeUtils.isDev) {
            this.controls.addEventListener('change', () => {
                localStorage.lookAt = JSON.stringify(this.getLookAt());
            });
        }

        if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
            this.resetLookAt({
                center: new Vector3(-5.774, -4.394, 8.691),
                position: new Vector3(-41.922, 31.348, -32.831)
            });
        }

        this.renderer.outputColorSpace = LinearSRGBColorSpace;

    }

    // 库房
    warehouse = (() => {
        const warehouse = new Warehouse();
        this.scene.add(warehouse);
        return warehouse;
    })();

    // 人物
    people = (() => {
        const people = new People();
        this.scene.add(people);
        people.position.set(37.191, 0.166, 1.226);

        // -36.878,0.167,1.285

        // people.awaitLoaded().then(() => {
        //     people.setAnimas("walk");
        // });
        return people;
    })();

    // people
    peopleTransition = (() => {
        const transition = new Transition({ x: 0, y: 0, z: 0 });
        transition.setDuration(70000);
        transition.setBezier([0, 0, 1, 1]);
        transition.onChange(({ x, y, z }) => {
            this.people.position.set(x, y, z);
        });
        return transition;
    })();

    // dispose1 = autorun(async () => {

    //     this.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
    //     this.people.lookAt(new Vector3(-36.878, 0.167, 1.285));

    //     this.followingPeople = false;

    //     if (homeStore.inspecting) {
    //         this.people.setAnimas("walk");

    //         await this.setLookAt({
    //             center: new Vector3(37.191, 0.166, 1.226),
    //             position: new Vector3(37.191, 0.166, 1.226).add(new Vector3(10, 10, 0))
    //         });

    //         this.followingPeople = true;

    //         while (homeStore.inspecting) {
    //             if (homeStore.inspecting) {
    //                 await this.peopleTransition.set({ x: -36.878, y: 0.167, z: 1.285 });
    //                 this.people.lookAt(new Vector3(37.191, 0.166, 1.226));
    //             }
    //             if (homeStore.inspecting) {
    //                 await this.peopleTransition.set({ x: 37.191, y: 0.166, z: 1.226 });
    //                 this.people.lookAt(new Vector3(-36.878, 0.167, 1.285));
    //             }
    //         }

    //     } else {
    //         this.people.setAnimas("idle");

    //         this.setLookAt({
    //             center: new Vector3(-5.774, -4.394, 8.691),
    //             position: new Vector3(-41.922, 31.348, -32.831)
    //         });

    //     }
    // });

    // 相机跟随人物的状态
    followingPeople = false;

    render(): void {

        super.render();

        if (this.followingPeople) {
            const { center, position } = this.getLookAt();
            const offset = this.people.position.clone().sub(center);
            console.log(offset);
            this.controls.target.copy(this.people.position);
            this.getCamera().position.add(offset);
        }

    }

}

window.addEventListener("message", async (e) => {
    // 根据消息内容执行相应操作
    if (e) {
        switch (e.data) {
            // 开始巡检
            case "startInspection":
                homeStore.setInspect(true);
                homeRenderer.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
                homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));

                homeRenderer.followingPeople = false;

                homeRenderer.people.setAnimas("walk");

                await homeRenderer.setLookAt({
                    center: new Vector3(37.191, 0.166, 1.226),
                    position: new Vector3(37.191, 0.166, 1.226).add(new Vector3(10, 10, 0))
                });

                homeRenderer.followingPeople = true;

                while (homeStore.inspecting) {
                    if (homeStore.inspecting) {
                        await homeRenderer.peopleTransition.set({ x: -36.878, y: 0.167, z: 1.285 });
                        homeRenderer.people.lookAt(new Vector3(37.191, 0.166, 1.226));
                    }
                    if (homeStore.inspecting) {
                        await homeRenderer.peopleTransition.set({ x: 37.191, y: 0.166, z: 1.226 });
                        homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));
                    }
                }
                break;

            // 巡检结束
            case "endInspection":
                homeStore.setInspect(false);
                homeRenderer.people.setAnimas("idle");
                homeRenderer.setLookAt({
                    center: new Vector3(-5.774, -4.394, 8.691),
                    position: new Vector3(-41.922, 31.348, -32.831)
                });
                break;

            // 开始盘点
            case "startInventory":
                homeStore.setInventory(true);
                homeRenderer.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
                homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));

                homeRenderer.followingPeople = false;
                // 移动视角
                await homeRenderer.setLookAt({
                    center: new Vector3(37.191, 0.166, 1.226),
                    position: new Vector3(37.191, 0.166, 1.226).add(new Vector3(0, 10, -10))
                });
                homeRenderer.people.setAnimas("walk");

                homeRenderer.followingPeople = true;

                //人物开始走路
                if (!homeStore.inventory) return;
                await homeRenderer.peopleTransition.setDuration(5000);
                await homeRenderer.peopleTransition.set({ x: 31.9, y: 0.166, z: 1.014 });
                if (!homeStore.inventory) return;
                homeRenderer.people.lookAt(new Vector3(31.9, 0.166, 2));
                if (!homeStore.inventory) return;
                homeRenderer.people.setAnimas("idle");
                if (!homeStore.inventory) return;
                await homeRenderer.warehouse.canbinetTransition.set({ x: 0.6 });
                if (!homeStore.inventory) return;

                // 走到第一处
                homeRenderer.people.setAnimas("walk");
                await homeRenderer.peopleTransition.setDuration(3000);
                if (!homeStore.inventory) return;
                await homeRenderer.peopleTransition.set({ x: 31.9, y: 0.166, z: 5 });
                if (!homeStore.inventory) return;
                homeRenderer.people.lookAt(new Vector3(32, 0.166, 5));
                homeRenderer.people.setAnimas("idle");
                await new Promise(resolve => setTimeout(resolve, 2000));
                
                // 走到第二处
                homeRenderer.people.lookAt(new Vector3(32, 0.166, 6));
                homeRenderer.people.setAnimas("walk");
                await homeRenderer.peopleTransition.setDuration(2000);
                if (!homeStore.inventory) return;
                await homeRenderer.peopleTransition.set({ x: 31.9, y: 0.166, z: 7 });
                if (!homeStore.inventory) return;
                homeRenderer.people.lookAt(new Vector3(33, 0.166, 7));
                homeRenderer.people.setAnimas("idle");

                await new Promise(resolve => setTimeout(resolve, 5000));
                // 结束盘点
                window.postMessage('endInventory', "*");
                break;

            // 盘点结束
            case "endInventory":
                homeStore.setInventory(false);
                homeRenderer.followingPeople = false;
                homeRenderer.people.setAnimas("idle");
                homeRenderer.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
                homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));
                homeRenderer.warehouse.canbinetTransition.set({ x: 0 });
                homeRenderer.setLookAt({
                    center: new Vector3(-5.774, -4.394, 8.691),
                    position: new Vector3(-41.922, 31.348, -32.831)
                });
                break;

            //开始查档
            case "startCheck":
                homeStore.setInventory(true);
                homeRenderer.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
                homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));

                homeRenderer.followingPeople = false;
                // 移动视角
                await homeRenderer.setLookAt({
                    center: new Vector3(37.191, 0.166, 1.226),
                    position: new Vector3(37.191, 0.166, 1.226).add(new Vector3(0, 10, -10))
                });
                homeRenderer.people.setAnimas("walk");

                homeRenderer.followingPeople = true;

                //人物开始走路
                if (!homeStore.inventory) return;
                await homeRenderer.peopleTransition.setDuration(5000);
                await homeRenderer.peopleTransition.set({ x: 31.9, y: 0.166, z: 1.014 });
                if (!homeStore.inventory) return;
                homeRenderer.people.lookAt(new Vector3(31.9, 0.166, 2));
                if (!homeStore.inventory) return;
                homeRenderer.people.setAnimas("idle");
                if (!homeStore.inventory) return;
                await homeRenderer.warehouse.canbinetTransition.set({ x: 0.6 });
                if (!homeStore.inventory) return;
                homeRenderer.people.setAnimas("walk");
                await homeRenderer.peopleTransition.setDuration(3000);
                if (!homeStore.inventory) return;
                await homeRenderer.peopleTransition.set({ x: 31.9, y: 0.166, z: 4 });
                if (!homeStore.inventory) return;
                homeRenderer.people.lookAt(new Vector3(32, 0.166, 4));
                homeRenderer.people.setAnimas("idle");

                await new Promise(resolve => setTimeout(resolve, 5000));
                // 结束盘点
                window.postMessage('endCheck', "*");
                break;

            //结束查档
            case "endCheck":
                homeStore.setInventory(false);
                homeRenderer.followingPeople = false;
                homeRenderer.people.setAnimas("idle");
                homeRenderer.peopleTransition.reset({ x: 37.191, y: 0.166, z: 1.226 });
                homeRenderer.people.lookAt(new Vector3(-36.878, 0.167, 1.285));
                homeRenderer.warehouse.canbinetTransition.set({ x: 0 });
                homeRenderer.setLookAt({
                    center: new Vector3(-5.774, -4.394, 8.691),
                    position: new Vector3(-41.922, 31.348, -32.831)
                });
                break;

            default:
                break;
        }
    }
});

const homeRenderer = new HomeRender();

export default homeRenderer;
